package com.eclink.asset.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eclink.asset.domain.AssetCategory;
import com.eclink.asset.mapper.AssetCategoryMapper;
import com.eclink.asset.pojo.query.AssetCategoryQuery;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * 资产分类表Service业务层处理
 *
 *
 * @author zcc
 */
@Service
public class AssetCategoryService {

    @Resource
    private AssetCategoryMapper assetCategoryMapper;

    private String getFormatNumber(int num, String parentNumber) {
        String pre = "0000";
        int preLength = pre.length();
        String str = pre + num;
        int strLength = str.length();
        String formatStr = str.substring(strLength - preLength, strLength);
        return StringUtils.isBlank(parentNumber) ? formatStr : parentNumber + "-" + formatStr;
    }

    public String getLastNumberFormat(Integer parentId, String parentNumber) {
        int lastNumber = this.getLastNumber(parentId);
        return this.getFormatNumber(lastNumber+1, parentNumber);
    }

    private int getLastNumber(Integer parentId) {
        QueryWrapper<AssetCategory> qw = new QueryWrapper<>();
        qw.eq("del_flag",0);

        if (parentId == null) {
            parentId = 0;
        }

        // 根据父节点查询
        qw.eq("parent_id", parentId);
        qw.last("limit 1");
        qw.orderByDesc("number");
        int num = 0;

        List<AssetCategory> list = assetCategoryMapper.selectList(qw);
        if (list == null || list.size() == 0) {
            return num;
        }
        AssetCategory one = list.get(0);
        String number = one.getNumber();
        String[] numberArr = number.split("-");
        String numberLast = numberArr[numberArr.length-1];
        return Integer.parseInt(numberLast);

    }

    /**
     * 查询资产分类表
     *
     * @param id 资产分类表主键
     * @return 资产分类表
     */
    public AssetCategory selectById(Serializable id) {
        return assetCategoryMapper.selectById(id);
    }

    @Cacheable(value="assetCategoryNameCache", key="#id")
    public String selectNameById(String id) {
        if (StringUtils.isBlank(id)) {
            return "";
        }
        return selectNameById(Integer.parseInt(id));
    }

    public String selectNameById(Serializable id) {
        AssetCategory one = selectById(id);
        if (null == one) {
            return null;
        }
        return one.getName();
    }

    /**
     * 查询资产分类表列表
     *
     * @param parentId 父节点ID
     * @return 资产分类表
     */
    public List<AssetCategory> getListByParentId(String parentId) {

        QueryWrapper<AssetCategory> qw = new QueryWrapper<>();
        qw.eq("del_flag",0);

        if (StringUtils.isBlank(parentId)) {
            parentId = "0";
        }

        qw.eq("parent_id", parentId);

        return assetCategoryMapper.selectList(qw);
    }

    /**
     * 查询资产分类表列表
     *
     * @param query 查询条件
     * @param page 分页条件
     * @return 资产分类表
     */
    public List<AssetCategory> selectList(AssetCategoryQuery query, Pageable page) {
        if (page != null) {
            PageHelper.startPage(page.getPageNumber() + 1, page.getPageSize());
        }
        QueryWrapper<AssetCategory> qw = new QueryWrapper<>();
        qw.eq("del_flag",0);

        String parentNumber = query.getParentNumber();
        if (StringUtils.isNotBlank(parentNumber)) {
            qw.likeRight("number", parentNumber+'-');
        }

        return assetCategoryMapper.selectList(qw);
    }

    /**
     * 查询资产分类表列表
     *
     * @return 资产分类表
     */
    public List<AssetCategory> selectListAll() {

        QueryWrapper<AssetCategory> qw = new QueryWrapper<>();
        qw.eq("del_flag",0);
        return assetCategoryMapper.selectList(qw);
    }

    /**
     * 新增资产分类表
     *
     * @param assetCategory 资产分类表
     * @return 结果
     */
    public int insert(AssetCategory assetCategory) {

        assetCategory.setDelFlag(0);
        return assetCategoryMapper.insert(assetCategory);
    }

    /**
     * 修改资产分类表
     *
     * @param assetCategory 资产分类表
     * @return 结果
     */
    public int update(AssetCategory assetCategory) {
        return assetCategoryMapper.updateById(assetCategory);
    }

    /**
     * 批量删除资产分类表
     *
     * @param ids 需要删除的资产分类表主键
     * @return 结果
     */
    public int deleteByIds(Long[] ids) {
        for(Long id : ids) {
            assetCategoryMapper.deleteById(id);
        }
        return 1;
    }

    /**
     * 删除资产分类表信息
     *
     * @param id 资产分类表主键
     * @return 结果
     */
    public int deleteById(Long id) {
        Long[] ids = {id};
        return assetCategoryMapper.deleteById(id);
    }


}
